import java.util.Arrays;

public class Solution2049 {

    long max=0;
    int ans=0;

    public int countHighestScoreNodes(int[] parents) {
        int[] left=new int[parents.length];
        int[] right=new int[parents.length];
        Arrays.fill(left,-1);
        Arrays.fill(right,-1);
        for (int i=1;i<parents.length;i++){
            if (left[parents[i]]==-1){
                left[parents[i]]=i;
            }else{
                right[parents[i]]=i;
            }
        }
        dfs(0,left,right);
        return ans;
    }

    private int dfs(int i,int[] left,int[] right) {
        if (left[i]==-1 && right[i]==-1){
            if (left.length-1>max){
                max=left.length-1;
                ans=1;
            }else if (left.length-1==max){
                ans++;
            }
            return 1;
        }
        int l=left[i]==-1?0:dfs(left[i],left,right);
        int r=right[i]==-1?0:dfs(right[i],left,right);
        int last=left.length-1-l-r;
        long an=1;
        if (l!=0){
            an=an*l;
        }
        if (r!=0){
            an=an*r;
        }
        if (last!=0){
            an=an*last;
        }
        if (an>max){
            max=an;
            ans=1;
        }else if (an==max){
            ans++;
        }
        return l+r+1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2049().countHighestScoreNodes(new int[]{-1,2,0,2,0}));
    }
}
